1760F - Quests - CodeForces Solution


binary search greedy sortings *1500

Please click on ads to support us..

Python Code:

T = int(input())

for hasbulla in range(T):
	n,c,d = [int(x) for x in map(int, input().split())]
	t = list(map(int, input().split()))
	t.sort(reverse = True)
	if(t[0]*d < c):
		print("Impossible")
		continue
	l = 0
	r = d+9
	ans = 0
	while l != r:
		mid = (l + r) // 2
		essa = 0
		for i in range(d):
			if i % mid < n:
				essa += t[i % mid]
		if essa >= c:
			l = mid+1
			ans = mid
		else:
			r = mid
	if(ans == d+8):
		print("Infinity")
		continue
	print(ans-1)

'''

6
2 5 4
1 2
2 20 10
100 10
3 100 3
7 2 6
4 20 3
4 5 6 7
4 100000000000 2022
8217734 927368 26389746 627896974
2 20 4
5 1

'''

C++ Code:

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define pb push_back
#define vll vector<ll>
#define vpl vector<pair<ll, ll>>
#define all(x) x.begin(), x.end()
#define fr(n) for(ll i=0;i<(n);i++)
#define frr(k, n) for(ll j=(k); j<(n); j++)
#define lcm(a,b) (a*b)/__gcd(a,b)
#define lz(x) 31-__builtin_clz(x)
#define endl "\n"
#define ff first
#define ss second
using namespace std;
mt19937 rng((ll)std::chrono::steady_clock::now().time_since_epoch().count());
const ll N=2e5+5, mod=1e9+7;

void error(){
    ll n, c, d; cin>>n>>c>>d;
    vll a(n); fr(n){cin>>a[i];}

    sort(all(a), greater<ll>());

    ll r=0;
    for(ll i=0; i<n && i<d; i++){
        r+=a[i];
    }
    if(a[0]*d < c){
        cout<<"Impossible"<<endl;
        return;
    }
    if(r>=c){
        cout<<"Infinity"<<endl;
        return;
    }
    ll l=0, h=d;
    while(l<=h){
        ll mid=(l+h)/2;
        ll rem=0;
        for(ll i=0; i<n&&i<=mid; i++){
            rem+=a[i];
        }
        ll r_rem=(d/(mid+1)), g=(d%(mid+1));
        rem*=r_rem;
        for(ll i=0; i<n&&i<g; i++){
            rem+=a[i];
        }
        if(rem<c){
            h=mid-1;
        }
        else{
            l=mid+1;
        }
    }
    cout<<l-1<<endl;
}

int main(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
int t=1;
cin>>t;
while(t--){error();}
return 0;
}


Comments

Submit
0 Comments
More Questions

50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem
432D - Prefixes and Suffixes
486A - Calculating Function
1373B - 01 Game
1187A - Stickers and Toys
313B - Ilya and Queries
579A - Raising Bacteria
723A - The New Year Meeting Friends
302A - Eugeny and Array
1638B - Odd Swap Sort
1370C - Number Game
1206B - Make Product Equal One